<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class VoiceCloneController extends Controller
{
    protected $host = 'https://openspeech.bytedance.com';

    /**
     * 上传音频文件进行语音克隆训练
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function train(Request $request)
    {
        try {
            $request->validate([
                'audio' => 'required|file|mimes:mp3,wav,m4a|max:10240', // 支持mp3、wav和m4a格式，最大10MB
                'speaker_id' => 'required|string',
            ]);

            $audioFile = $request->file('audio');
            $encodedData = base64_encode(file_get_contents($audioFile->getRealPath()));
            $audioFormat = $audioFile->getClientOriginalExtension();

            $response = Http::withHeaders([
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer;' . config('services.bytedance.token'),
                'Resource-Id' => 'volc.megatts.voiceclone',
            ])->post($this->host . '/api/v1/mega_tts/audio/upload', [
                'appid' => config('services.bytedance.appid'),
                'speaker_id' => $request->speaker_id,
                'audios' => [
                    [
                        'audio_bytes' => $encodedData,
                        'audio_format' => $audioFormat
                    ]
                ],
                'source' => 2,
                'language' => 0,
                'model_type' => 1
            ]);

            if (!$response->successful()) {
                throw new \Exception('训练请求错误: ' . $response->body());
            }

            return response()->json([
                'success' => true,
                'data' => $response->json(),
                'message' => '音频上传成功'
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * 获取语音克隆训练状态
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getStatus(Request $request)
    {
        try {
            $request->validate([
                'speaker_id' => 'required|string',
            ]);

            $response = Http::withHeaders([
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer;' . config('services.bytedance.token'),
                'Resource-Id' => 'volc.megatts.voiceclone',
            ])->post($this->host . '/api/v1/mega_tts/status', [
                'appid' => config('services.bytedance.appid'),
                'speaker_id' => $request->speaker_id
            ]);

            return response()->json([
                'success' => true,
                'data' => $response->json()
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    }
} 